local shut_up = GetModConfigData("sw_well") == "shutup"
local function TIPS(content, color)
    if not shut_up then
        TIP(GLOBAL.STRINGS.NAMES.WAXWELLJOURNAL, color, content, "head")
    end
end

local function getTheBook()
    local books = GetItemsFromAll("waxwelljournal")
    for _,book in pairs(books)do
        if GetPerc(book) > 0 then
            return book
        end
    end
end

local function setBind(key, num)
    if key then
        GLOBAL.TheInput:AddKeyUpHandler(key, function()
            if InGame() and GLOBAL.ThePlayer.prefab == "waxwell" then
                local flag = false
                local thebook = getTheBook()
                if thebook and thebook.components then
                    local spell = thebook.components.spellbook 
                    if num <= 4 then
                        local power = spell and spell.items[num]
                        if power and power.onselect and power.execute then
                            spell:SelectSpell(num)
                            power.onselect(thebook)
                            power.execute(thebook)
                            flag = true
                            TIPS(spell:GetSpellName(), "green")
                        end
                    elseif num == 5 then
                        local hud = GLOBAL.ThePlayer.HUD
                        if spell and hud then
                            if hud:IsSpellWheelOpen() then
                                hud:CloseSpellWheel()
                            else
                                spell:OpenSpellBook(GLOBAL.ThePlayer)
                            end
                            flag = true
                        end
                    end
                end
                if not flag then
                    TIPS(GLOBAL.STRINGS.CHARACTERS.GENERIC.ACTIONFAIL_GENERIC, "pink")
                end
            end
        end)
    end
end


for i=1,5 do setBind(GetModConfigData("well_bind"..i), i) end

local keybind6 = GetModConfigData("well_bind6")

if keybind6 then
    GLOBAL.TheInput:AddKeyUpHandler(keybind6, function()
        if InGame() and GLOBAL.ThePlayer.prefab == "waxwell" then
            local tophat = GetItemFromAll("tophat", {"shadowlevel"})
            local head = GetEquippedItemFrom(GLOBAL.EQUIPSLOTS and GLOBAL.EQUIPSLOTS.HEAD)
            local flag
            if tophat then
                local controller = GLOBAL.ThePlayer.components.playercontroller
                local act = GLOBAL.ACTIONS.USEMAGICTOOL
                local buff_act = GLOBAL.BufferedAction(GLOBAL.ThePlayer, nil, act, tophat)
                if act and buff_act then
                    buff_act.preview_cb = function()
                        GLOBAL.SendRPCToServer(GLOBAL.RPC.UseItemFromInvTile, act.code, tophat, head ~= tophat and 1 or nil)
                    end
                    if controller.locomotor then
                        controller:DoAction(buff_act)
                    else
                        buff_act.preview_cb()
                    end
                    flag = true
                end
            end
            if not flag then
                if not shut_up then
                    TIP(GLOBAL.STRINGS.NAMES.TOPHAT_MAGICIAN, "pink", GLOBAL.STRINGS.CHARACTERS.GENERIC.ACTIONFAIL_GENERIC, "head")
                end
            end
        end
    end)
end